package com.cn.xiongmeng.czlxz.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

public class IntervalControler {
    public static void intervalControlForRewardAd(Context context, Runnable controlledFunc, String key1, int interval1, String key2, int interval2){
        SharedPreferences prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);

        long preKey1Time = prefs.getLong(key1, 0);
        long preKey2Time = prefs.getLong(key2, 0);

        long curDate = System.currentTimeMillis();

        if (preKey1Time != 0) {
            long interval = (curDate - preKey1Time) / 1000;
            if (interval < interval1) { // 不能点击
                long waitTime = interval1 - interval;

                if (preKey2Time != 0) {
                    interval = (curDate - preKey2Time) / 1000;
                    long rewardWaitTime = interval2 - interval;
                    waitTime = Math.max(rewardWaitTime, waitTime);
                }
                Toast.makeText(context, "请等待：" + waitTime + "s", Toast.LENGTH_SHORT).show();
            } else { // 可点击
                SharedPreferences.Editor editor = prefs.edit();

                editor.putLong(key1, curDate);
                editor.apply();

                if (preKey2Time != 0) { // 不可调用
                    interval = (curDate - preKey2Time) / 1000;
                    if (interval < interval2) {
                        long waitTime = interval2 - interval;
                        Toast.makeText(context, "请等待：" + waitTime + "s", Toast.LENGTH_SHORT).show();
                    } else {
                        controlledFunc.run();
                    }
                } else { // 可调用
                    controlledFunc.run();
                }
            }
        } else {
            SharedPreferences.Editor editor = prefs.edit();

            editor.putLong(key1, curDate);
            editor.apply(); // apply异步，commit同步

            controlledFunc.run();
        }
    }
    public static void intervalControlForObtainingTl(Context context, Runnable controlledFunc, String key1, int interval1){
        SharedPreferences prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);

        long preKey1Time = prefs.getLong(key1, 0);
        long curDate = System.currentTimeMillis();
        if (preKey1Time != 0) {
            long interval = (curDate - preKey1Time) / 1000;
            if (interval < interval1) { // 不能点击
                long waitTime = interval1 - interval;

                Toast.makeText(context, "请等待：" + waitTime + "s", Toast.LENGTH_SHORT).show();
            } else { // 可点击
                SharedPreferences.Editor editor = prefs.edit();

                editor.putLong(key1, curDate);
                editor.apply();

                controlledFunc.run();
            }
        } else {
            SharedPreferences.Editor editor = prefs.edit();

            editor.putLong(key1, curDate);
            editor.apply(); // apply异步，commit同步

            controlledFunc.run();
        }
    }
    public static void intervalControlForStartAPP(Context context, Runnable controlledFunc, String key1, int interval1){
        SharedPreferences prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);

        long preKey1Time = prefs.getLong(key1, 0);

        long curDate = System.currentTimeMillis();

        if (preKey1Time != 0){
            long interval = (curDate - preKey1Time) / 1000;
            if (interval < interval1) { // 提示、退出APP
                long waitTime = interval1 - interval;

                ContextCompat.getMainExecutor(context).execute(() -> {
                    // 这里是 UI 代码
                    Toast.makeText(context, "请勿频繁启动，等待：" + waitTime + "s", Toast.LENGTH_LONG).show();
                });
                controlledFunc.run();
            } else{ // 缓存
                SharedPreferences.Editor editor = prefs.edit();

                editor.putLong(key1, curDate);
                editor.apply(); // apply异步，commit同步
            }
        } else { // 缓存
            SharedPreferences.Editor editor = prefs.edit();

            editor.putLong(key1, curDate);
            editor.apply(); // apply异步，commit同步
        }
    }
}